﻿$(function() {

    //Initlist(document.body);

    //    p = $('#projectList').datagrid('getPager');
    //    if (p) {
    //        $(p).pagination({
    //            displayMsg: '显示 {from} 到 {to} 共 {total} 记录'
    //        });
    //    }
    param.Options["Internaltype"] = "1";
    autoResize({ dataGrid: '#projectList', callback: Initlist });
    window_edit = $('#Add').window({
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closable: true,
        width: 600,
        //        height: 680,
        closed: true,
        onOpen: function() {
            $('#Add').parent().css("left", "10%");
            $('#Add').parent().next().css("left", "10%");
            $('#Add').parent().next().css("top", "10%");
            $('#Add').parent().css("top", "10%");
        }
    });
    JSUtility.window_edit_title = "项目基本信息";
    window_edit_form = $("#Add").find("form");
    $('#Add').show();
});
function bodyUnload() {
    comboboxInitial();
    dateboxInitial();
};
function comboboxInitial() {
    $('#ProjectType').combobox({//项目类型
        url: "../../Handler/AjaxComboboxData.ashx?typeId=15",
        valueField: 'text',
        textField: 'text'
    });
    JSHelper.comboboxData2("#ProjectProperty", "4");
    JSHelper.comboboxData2("#BrakeMode", "14");
    //    $('#ProjectProperty').combobox({//项目属性
    //        url: '../../Handler/AjaxComboboxData.ashx?typeId=4',
    //        valueField: 'text',
    //        textField: 'text'
    //    });
    $('#ProjectLevel').combobox({//项目级别
        url: '../../Handler/AjaxComboboxData.ashx?typeId=5',
        valueField: 'text',
        textField: 'text'
    });
    JSHelper.ajaxComboCheck('../../Handler/AjaxComboboxData.ashx?typeId=16', 'sp1', 'ProjectPhase');
    JSHelper.ajaxComboCheck('../../Handler/AjaxComboboxData.ashx?typeId=17', 'sp2', 'ProjectField');
    //    $('#ProjectPhase').combobox({//项目阶段
    //        url: '../../Handler/AjaxComboboxData.ashx?typeId=16',
    //        valueField: 'text',
    //        textField: 'text',
    //        required: true,
    //        editable: false,
    //        multiple: true

    //    });
    //    $('#ProjectField').combobox({ //项目涉及专业
    //        url: '../../Handler/AjaxComboboxData.ashx?typeId=17',
    //        valueField: 'text',
    //        textField: 'text',
    //        required: true,
    //        editable: false,
    //        multiple: true
    //    });
    // $('#sp1').appendTo($('#ProjectPhase').combo('panel'));
    //$('#sp2').appendTo($('#ProjectSpecialize').combo('panel'));
    //    $('#sp1> input').click(comboCheckValue);
    //JSHelper.ajaxComboCheck("", 'sp1', 'ProjectStage');
    comboGridInit();
}

function dateboxInitial() {
    $("#ProjectBriefTime").datebox();
    $('#CreateDate').datebox();
    $('#PlanStartTime').datebox();
    $('#PlanOpenTime').datebox();
    $('#DesignStartTime').datebox();
    $('#DesignOpenTime').datebox();
    //  $('#ProjectImportance').datebox();
}

function comboGridInit() {
    $('#ProjectPrincipal').combogrid({
        panelWidth: 400,
        value: '006',

        idField: 'Id',
        textField: 'EmployeeName',
        url: '',
        columns: [[
					{ field: 'Id', title: '编号', width: 60 },
					{ field: 'EmployeeCardID', title: '人员编码', width: 100 },
					{ field: 'EmployeeName', title: '姓名', width: 120 },
					{ field: 'Department', title: '部门', width: 100 }
				]]
    });
    //"".split
}


function Initlist(size) {
    param.Method = "Paged";
    grid = $('#Grid').datagrid({
        tilte: '项目基本信息',
        iconCls: 'icon-save',
        width: size.width,
        height: size.height,
        pageSize: Math.round(size.height / 25),
        pageList: [Math.round(size.height / 25)],
        nowrap: true,
        striped: true,
        url: '../../Handler/AjaxHandle.ashx',
        queryParams: { "json": $.toJSON(param) },
        sortName: 'Id',
        sortOrder: 'desc',
        idField: 'Id',
        frozenColumns: [[{ title: '编号', field: 'Id', width: 50, sortable: true}]],
        columns: [[
                            { title: '项目编号', field: 'ProjectNum', width: 120 },
                            { title: '项目名称', field: 'ProjectName', width: 120 },
                            { title: '项目下达时间', field: 'ProjectBriefTime', width: 100 },
        //                            { title: '工程图号', field: 'ConstructionNum', width: 120 },
                            {title: '项目类型', field: 'ProjectType', width: 100 },
                            { title: '项目属性', field: 'ProjectProperty', width: 100 },
                            { title: '项目级别', field: 'ProjectLevel', width: 100 },
                            { title: '项目级别年度', field: 'ProjectImportance', width: 100 },
                            { title: '项目投资额', field: 'IsReply', width: 80 },
                            { title: '投资金额', field: 'Investment', width: 100 },
                            { title: '正线车站数', field: 'StationCount', width: 80 },
                            { title: '控制中心数', field: 'ControlCenterCount', width: 80 },
                            { title: '正线线路长度', field: 'LineLength', width: 80 },
                            { title: '厂段数量', field: 'SectionCount', width: 80 }, //12                            
                            {title: '项目创建时间', field: 'CreateTime', width: 100 }
                        ]],
        view: detailview,
        detailFormatter: function(rowIndex, rowData) {
            //多列只存储Text

            var doc = '<table id="gridview" cellpadding="0" cellspacing="0"><tr>' +
	            '<td style="width:100px;">项目阶段：</td><td> ' + rowData.ProjectPhase + '</td>' +
	            '<td style="width:100px;">项目涉及专业：</td><td>  ' + rowData.ProjectField + '</td>' +
	            '<td style="width:100px;">实际配置列车数：</td><td> ' + rowData.TrainCount + '</td>' +
	            '<td style="width:100px;">计划开工时间：</td><td> ' + rowData.PlanStartTime + '</td>' +
	            '<td style="width:100px;">计划开通时间：</td><td> ' + rowData.PlanOpenTime + '</td>' +
            '</tr>' +
            '<tr>' +
	            '<td style="width:100px;">预算工时：</td><td>  ' + rowData.PrecastWorkHours + '</td>' +
	            '<td style="width:100px;">完成工时：</td><td> ' + rowData.WorkHours + '</td>' +
	            '<td style="width:100px;">剩余工时：</td><td> ' + rowData.SurplusWorkHours + '</td>' +
	            '<td style="width:100px;">项目所在省：</td><td> ' + rowData.ProjectProvince + '</td>' +
	            '<td style="width:100px;">项目所在市：</td><td> ' + rowData.ProjectCity + '</td>' +
            '</tr>' +
           '</table>';
            return doc;
        },
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [
                    {
                        text: '添加',
                        iconCls: 'icon-add',
                        handler: Add
                    }, '-',
                    {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: Update
                    }, '-',
                    {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: Del
                    }, '-',
                    {
                        text: '查询',
                        iconCls: 'icon-search',
                        handler: Search
                    }, '-',
                    {
                        text: '详细信息',
                        iconCls: 'icon-edit',
                        handler: function() {
                            row = JSHelper.isDatagridSelected($("#Grid"));
                            if (row == null) return;
                            self.parent.addTab('集成项目详细信息', "Project/IntegrationProject/ProjectInfoManage.htm", 'icon-edit');
                            self.parent.project_id = row.ProjectNum; //JSHelper.isDatagridSelected($('#projectList'));
                            self.parent.project_Name = row.ProjectName;
                        }
                    }
                  ]
    });
}

var operate;
var row;
var window_edit;
var window_edit_form;
var grid;
var window_type;
var param = { AssemblyName: "OA.Bll.Project", ClassName: "OA.Bll.Project.ProjectInfoPresenter", Method: "", Options: {} };

function Add() {
    $('#Add').window({ title: '添加项目基本信息', iconCls: 'icon-add', closed: false });
    window_edit_form.form("clear");
    JSHelper.clearComboCheck("sp1", 'ProjectPhase');
    JSHelper.clearComboCheck("sp2", 'ProjectField');
    $("#ProjectProvince").combobox("setValue", "北京");
    $("#IsReply").combobox("setValue", "未批复");
    operate = "add";
}
function Update() {
    row = JSHelper.isDatagridSelected($("#Grid"));
    if (row == null) return;
    JSHelper.clearComboCheck("sp1", 'ProjectPhase');
    JSHelper.clearComboCheck("sp2", 'ProjectField');
    window_edit_form.form("load", row);
    JSHelper.ajaxDataBindComboCheck(row.ProjectPhase, 'sp1', 'ProjectPhase');
    JSHelper.ajaxDataBindComboCheck(row.ProjectField, 'sp2', 'ProjectField');
    //    $("#ProjectPhase").combobox("setValues", row.ProjectPhase.split(","));
    //    $("#ProjectField").combobox("setValues", row.ProjectField.split(","));
    //alert(row.ProjectField.split(","));
    $('#Add').window({ title: '修改项目基本信息', iconCls: 'icon-edit', closed: false });
    operate = "update";
}
function Search() {
    $('#Add').window({ title: '查询项目基本信息', iconCls: 'icon-search', closed: false });
    window_edit_form.form("clear");
    JSHelper.clearComboCheck("sp1", 'ProjectPhase');
    JSHelper.clearComboCheck("sp2", 'ProjectField');
    operate = "search";
}
function EditProjectPrize() {
    // data = JSHelper.isDatagridSelected($('#projectList'));
    //if (data != null)
    JSHelper.addTab(null, '项目获奖信息', 'Project/ProjectInfoManage.htm');
}

function Save() {
    if (!window_edit_form.form("validate") && operate != "search") return;
    var items = window_edit_form.serializeArray();
    $.each(items, function(i, item) {
        param.Options[item.name] = item.value;
    });
    //多列列表只存储Text
    var projectPhase = $("#ProjectPhase").combobox("getValues").toString(); // $("#ProjectPhase").combobox("getValues") + "|" +
    var projectField = $("#ProjectField").combobox("getValues").toString(); //$("#ProjectSpecialize").combobox("getValues") + "|" +
    param.Options["ProjectPhase"] = projectPhase;
    param.Options["ProjectField"] = projectField;
    //alert($.toJSON(param.Options));
    if (operate == "add")
        param.Method = "Add";
    else if (operate == "update") {
        param.Method = "Update";
        param.Options["Id"] = row.Id;
    } else {
        window_edit.window("close");
        param.Method = "Search";
        grid.datagrid({ method: "post", queryParams: { "json": $.toJSON(param) }, url: "../../Handler/AjaxHandle.ashx" });
        grid.datagrid('options').pageNumber = 1;
        return;
    }

    $.ajax({
        type: "post",
        url: "../../Handler/AjaxHandle.ashx",
        data: $.toJSON(param),
        dataType: "json",
        success: function(data) {
            if (data.flag) {
                grid.datagrid('reload');
                $.messager.show({ msg: data.msg, title: "info" });
                window_edit.window('close');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

function Del() {
    row = JSHelper.isDatagridSelected($("#Grid"));
    if (row == null) return;
    $.messager.confirm("删除提示", "是否确认要删除该条信息?", function(b) {
        if (b) {
            param.Method = "Delete";
            param.Options["Id"] = row.Id;
            $.ajax({
                type: "post",
                url: "../../Handler/AjaxHandle.ashx",
                data: $.toJSON(param),
                dataType: "json",
                success: function(data) {
                    if (data.flag) {
                        grid.datagrid('reload');
                        $.messager.show({ msg: data.msg, title: "info" });
                        //$.messager.alert('提示', data.msg, 'info');
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            });
        }
    });
}